﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using ClassLibrary.Data;
using ClassLibrary.Entity;
using ClassLibrary.DAO;
using System.Data;


/// <summary>
/// Summary description for BasePage
/// </summary>
public class BasePage : System.Web.UI.Page
{
    public BasePage()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    /// <summary>
    /// Set Shopping Cart Item and Price to Session
    /// </summary>
    /// <param name="ProductPrice"></param>
    /// <returns>ProductId|Price</returns>
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]    
    public static string SetShoppingCartSesstion(string ProductCodeAndPrice)
    {
        try
        {
            string strResult = String.Empty;
            if (ProductCodeAndPrice != String.Empty)
            {
                string[] strCodeAndPrice = ProductCodeAndPrice.Split('|');
                
                
                TblOrderDetail order_detail = new TblOrderDetail();                
                
                TblItemsDAO itemsDAO = new TblItemsDAO(Global.CONNECTION_STRING);

                TblItems item = itemsDAO.GetItemByID(int.Parse(strCodeAndPrice[0].Trim()));
                //DataTable item = itemsDAO.GetvItemByID(int.Parse(strCodeAndPrice[0].Trim()));
                //order_detail.ID = 1; 
                //foreach (DataRow dr in item.Rows)
                //{
                //    order_detail.ID = 1;
                //    order_detail.ItemID = Convert.ToInt32(dr["ItemID"]);
                //    //order_detail.Item = item;
                //    order_detail.Price = Convert.ToDouble(dr["BaseSellingPrice"]);
                //}
                order_detail.ID = 1;
                order_detail.ItemID = item.ItemID;
                order_detail.Items = item;
                order_detail.Price = item.BaseSellingPrice;
                    
                order_detail.Quantity = 1;


                //order_detail.Price = double.Parse(strCodeAndPrice[1].Trim());
                //order_detail.Price = item.BaseSellingPrice;
                //order_detail.TotalAmount = order_detail.Quantity * order_detail.Price;

                List<TblOrderDetail> lstItems = null;
                //Initiate the shopping cart
                if (HttpContext.Current.Session[Global.SESSION_CART] == null)
                {
                    lstItems = new List<TblOrderDetail>();
                    //lstItems.Add(order_detail);
                }
                else //Add more item to the exist shopping cart
                {
                    lstItems = (List<TblOrderDetail>)HttpContext.Current.Session[Global.SESSION_CART];
                    //AddItemToCurrentCart(ref lstItems, order_detail);
                }

                AddItemToCurrentCart(ref lstItems, order_detail);
                HttpContext.Current.Session[Global.SESSION_CART] = lstItems;
                strResult = BuildItemsAndAmount(lstItems);
                

                //DBService dbSrv = new DBService(Global.CONNECTION_STRING);
                ////TbItem item = TbItem.GetItemById(dbSrv, int.Parse(strCodeAndPrice[0].Trim()));
                //TbItem item = new TbItem();
                //if (item != null)
                //{
                //    item.ShoppingQuantity++;
                    
                //    ArrayList arrItems = null;
                //    //Initiate the shopping cart
                //    if (HttpContext.Current.Session[Global.SESSION_CART] == null)
                //    {
                //        arrItems = new ArrayList();
                //        arrItems.Add(item);
                //    }
                //    else //Add more item to the exist shopping cart
                //    {
                //        arrItems = (ArrayList)HttpContext.Current.Session[Global.SESSION_CART];
                //        AddItemToCurrentCart(ref arrItems, item);
                //    }
                //    HttpContext.Current.Session[Global.SESSION_CART] = arrItems;
                //    strResult = BuildItemsAndAmount(arrItems);
                //}
            }

            return strResult;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public static void AddItemToCurrentCart(ref List<TblOrderDetail> ItemsList, TblOrderDetail order_detail)
    {
        bool bolExist = false;
        try
        {            
            foreach (TblOrderDetail o in ItemsList)
            {
                if (o.ItemID == order_detail.ItemID)
                {
                    o.Quantity++;
                    o.TotalAmount = o.Quantity * o.Price;
                    bolExist = true;
                    return;
                }
            }
            if(!bolExist)
                ItemsList.Add(order_detail);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private static string BuildItemsAndAmount(List<TblOrderDetail> ItemsList)
    {
        try
        {
            int iQuantity = 0;
            double dAmount = 0;
            foreach (TblOrderDetail order in ItemsList)
            {
                iQuantity += order.Quantity;
                //dAmount += order.TotalAmount;
                dAmount += order.Quantity * order.Price;
            }

            return iQuantity.ToString() + "|" + dAmount.ToString("0,00.##");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
